package com.cgy.utils.elasticsearch.bulk;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * @author CH
 */
@Slf4j
public class ElasticSearchBulkUpdate {
    /**
     *
     * @param response
     * @return
     */
    public static List<BulkItemResponse.Failure> response(BulkResponse response) {
        List<BulkItemResponse.Failure> failureList = new ArrayList<>();
        // 全部操作成功
        if (!response.hasFailures()) {
            log.info("批量更新操作成功！");
        } else {
            for (BulkItemResponse bulkItemResponse : response) {
                if (bulkItemResponse.isFailed()) {
                    BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
                    log.error("\"index={}, type={}, id={}\"的文档更新失败！", failure.getIndex(), failure.getType(), failure.getId());
                    log.error("更新失败详情: {}", failure.getMessage());
                    failureList.add(failure);
                } else {
                    log.info("\"index={}, type={}, id={}\"的文档更新成功！", bulkItemResponse.getIndex(), bulkItemResponse.getType(), bulkItemResponse.getId());
                }
            }
        }
        return failureList;
    }
}
